const httpUtils = require('../../../utils/httpUtils')
const ui = require('../../../utils/ui')

Page({
  /**
   * 页面的初始数据
   */
  data: {
    notebookList: [],
    total: 0, //分页总数
    page: 1, //分页记录数
    pageSize: 10, //分页大小
    onRefresh: true,
    photoWidth: wx.getWindowInfo().windowWidth / 5,
    showTextAndTitleWithInput: false,
    item: {}
  },

  // 点击图片进行大图查看
  LookPhoto: function (e) {
    console.log(e);
    wx.previewImage({
      current: e.currentTarget.dataset.photurl,
      urls: e.currentTarget.dataset.list
    })
  },

  // 删除朋友圈
  delete: function (e) {
    // wx.showToast({
    //   title: '删除成功',
    // })
    let item = e.currentTarget.dataset.entity;
    console.log(item)
    this.setData({
      ['showTextAndTitleWithInput']: true,
      item: item
    })
  },

    //点击删除弹窗的确定
    confirmDelDic: function (e) {
      let that = this;
      let item = that.data.item;
      let obj = {
        method: 'GET',
        showLoading: false,
        url: 'notebook/delNotebook?id=' + item.id
      }
  
      httpUtils.request(obj).then(res => {
        console.log(res.data.message)
        if (res.data.retCode == 0) {
          that.setData({
            showTextAndTitleWithInput: false,
            item: {},
            onRefresh: false,
            page: 1,
            notebookList: []
          });
          that.getNotebookList();
        } else {
          ui.showToast(res.data.message)
        }
      }).catch(err => {
        ui.showToast('请求失败')
      });
    },

  //点击删除弹窗的取消
  closeDelDialog: function () {
    this.setData({
      ['showTextAndTitleWithInput']: false
    });
  },

  //分页查询日记列表
  getNotebookList: function () {
    let that = this;
    var onRefresh = that.data.onRefresh; //false为重新刷新数据，true为分页累加数据
    let obj = {
      method: 'POST',
      showLoading: false,
      url: 'notebook/query/page',
      data: {
        'page': that.data.page,
        'size': that.data.pageSize,
        'column': 'recordDate,createTime',
        'order': 'DESC'
      }
    }

    httpUtils.request(obj).then(res => {
      console.log(res.data.message)
      if (res.data.retCode == 0) {
        var datas = res.data.data.records;
        if (datas != null && datas.length > 0) {
          if (onRefresh) { //false为重新刷新数据，true为分页累加数据
            that.setData({
              notebookList: that.data.notebookList.concat(datas),
              page: res.data.data.current,
              total: res.data.data.pages
            })
          } else {
            that.setData({
              notebookList: datas,
              page: res.data.data.current,
              total: res.data.data.pages
            })
          }
        } else {
          ui.showToast('没有更多数据了')
        }
      } else {
        ui.showToast(res.data.message)
      }
    }).catch(err => {
      ui.showToast('请求失败' + err)
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getNotebookList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.setData({
      onRefresh: false
    });
    this.getNotebookList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    let that = this;
    this.setData({
      onRefresh: true,
      page: that.data.page + 1
    });
    this.getNotebookList();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})